import { actionCreator } from '@/library/libs/context/action-creator'

class ConfigCtxClass {
  /** Debug */
  static readonly CHANGE_DEBUG = 'CHANGE_DEBUG'

  /** Language */
  static readonly CHANGE_LANGUAGE = 'CHANGE_LANGUAGE'
}

const configCtxInitData = {
  openDebug: false,
  language: 'zh-CN',
}

type ConfigCtxInitDataType = typeof configCtxInitData

type ConfigCtxSetDebugActionType = {
  type: typeof ConfigCtxClass.CHANGE_DEBUG,
  flag: boolean
}

type ConfigCtxSetLanguageActionType = {
  type: typeof ConfigCtxClass.CHANGE_LANGUAGE,
  payload: string
}

const configCtxSetDebugAction = actionCreator<ConfigCtxSetDebugActionType>(ConfigCtxClass.CHANGE_DEBUG, 'flag')
const configCtxSetLanguageAction = actionCreator<ConfigCtxSetLanguageActionType>(ConfigCtxClass.CHANGE_LANGUAGE, 'payload')

const configCtxReducer = (
  state: ConfigCtxInitDataType,
  action: ConfigCtxSetDebugActionType | ConfigCtxSetLanguageActionType
): ConfigCtxInitDataType => {
  switch (action.type) {
  case ConfigCtxClass.CHANGE_DEBUG:
    return { ...state, openDebug: action.flag }
  case ConfigCtxClass.CHANGE_LANGUAGE:
    return { ...state, language: action.payload }
  default:
    return state
  }
}

export {
  configCtxInitData,
  configCtxSetDebugAction,
  configCtxSetLanguageAction,
  configCtxReducer,
}
